package com.simdo.g768.Dialog;


import static com.simdo.g768.MainActivity.device;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.g768.MessageControl.MessageController;
import com.simdo.g768.Bean.GnbBean;
import com.simdo.g768.Bean.StepBean;
import com.simdo.g768.MainActivity;
import com.simdo.g768.R;
import com.simdo.g768.Util.StatusBarUtil;

public class CustomDialog extends Dialog {

    private final String title;
    private final String content;
    private final Context mContext;
    private final View contentView;

    public CustomDialog(Context context, String title, String content) {
        super(context, R.style.style_dialog);
        this.mContext = context;
        contentView = View.inflate(context, R.layout.dialog_confirm, null);
        this.setContentView(contentView);
        this.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
        //this.setCancelable(false);   // 返回键不消失
        //设置dialog大小，这里是一个小赠送，模块好的控件大小设置
        Window window = this.getWindow();
        //window.setGravity(Gravity.BOTTOM); //可设置dialog的位置
        window.getDecorView().setPadding(0, 0, 0, 0); //消除边距
        StatusBarUtil.setLightStatusBar(window, true);
        WindowManager.LayoutParams lp = window.getAttributes();

        lp.width = context.getResources().getDisplayMetrics().widthPixels * 6 / 7;// 设置宽度屏幕的 6 / 7
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
        this.title = title;
        this.content = content;
        initView();
    }

    private void initView() {
        TextView tv_title = contentView.findViewById(R.id.tv_title); // 获取TextView视图
        tv_title.setText(title); // 设置TextView的文本内容

        TextView tv_msg = contentView.findViewById(R.id.tv_msg); // 获取TextView视图
        tv_msg.setText(content); // 设置TextView的文本内容

        TextView btn_ok = contentView.findViewById(R.id.btn_ok);
        btn_ok.setText("立即重启");
        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 在这里你可以执行用户确认后的操作
                if (device.getWorkState() != GnbBean.State.REBOOT) {
                    MainActivity.getInstance().updateSteps(StepBean.State.success, mContext.getString(R.string.rebooting));
                    device.setWorkState(GnbBean.State.REBOOT);
                    MessageController.build().rebootDev(device.getId());
                }
                dismiss(); // 关闭对话框
            }
        });

        contentView.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss(); // 关闭对话框
            }
        });
    }
}
